﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.Common;
using MacomberMapSystem.Common.CIM;
using MacomberMapSystem.Common.Serialization;
using MacomberMapSystem.Common.Exports;
using MacomberMapSystem.Common.Database;

namespace MacomberMapSystem.Common.User_Interfaces.One_Lines
{
    /// <summary>
    /// This class provides for the validation of a one-line
    /// </summary>
    public class MM_OneLineValidation : IComparable<MM_OneLineValidation>
    {
        #region Variable declarations
        /// <summary>Our XML document for our one-line validation</summary>
        public XmlDocument xDoc = new XmlDocument();

        /// <summary>The error count</summary>
        public int Errors;

        /// <summary>The warning count</summary>
        public int Warnings;

        /// <summary>The ID of our one-line</summary>
        public int ID;

        /// <summary>When the one-line was last modified</summary>
        public DateTime LastExported;

        /// <summary>The validation text</summary>
        public String ValidationText;
        #endregion

        #region Initialization
        /// <summary>
        /// Add a 'new element' one-line validator
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="ElemCount"></param>
        public MM_OneLineValidation(XmlDocument xDoc, int ElemCount)
        {
            this.xDoc = xDoc;
            Errors = ElemCount;
            ID = -1;
            this.LastExported = DateTime.MinValue;
            ValidationText = "New one-line";
        }

        /// <summary>
        /// Add a new one-line validation
        /// </summary>
        /// <param name="dRd"></param>
        /// <param name="Repository"></param>
        /// <param name="BaseElement"></param>
        public MM_OneLineValidation(DbDataReader dRd, CIM_Element BaseElement, MM_Repository Repository)
        {
            String InXml = (string)dRd["Document"];
            xDoc.LoadXml(InXml.TrimStart((Char)191));
            this.ID = Convert.ToInt32(dRd["ID"]);
            if (dRd["LastExported"] is DateTime)
                this.LastExported = Convert.ToDateTime(dRd["LastExported"]);
            else
                this.LastExported = DateTime.MinValue;
            MM_Database_OneLine TempOL = new MM_Database_OneLine(dRd, Repository);
            TempOL.Document = xDoc;
            ValidationText = MM_OneLine_Validation.ValidateTopology(xDoc.DocumentElement, BaseElement.CIM, new MM_Oneline_Export(TempOL, BaseElement.CIM), out Errors, out Warnings);
        }
        #endregion
        
        /// <summary>
        /// Compare two one-line validation results
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(MM_OneLineValidation other)
        {
            if (Errors == other.Errors)
                return Warnings.CompareTo(other.Warnings);
            else
                return Errors.CompareTo(other.Errors);
        }

        /// <summary>
        /// Report our one-line vlaidation results in an easy to read mode
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (ID == -1)
                return "NEW: Elems: " + Errors.ToString("#,##0");
            else
                return "Err: " + Errors.ToString("#,##0") + " / " + "Wrn: " + Warnings.ToString("#,##0") + " / ID: " + ID.ToString("#,##0");
        }
    }
}